%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Explanation of what the file does
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% This function solves for dot wages and labors (and also prices dot, trade 
% shares new levels and new revenue levels) of the one period temporary 
% equilibrium in the RUV model using an extended Alvarez & Lucas type 
% tatonnement algorithm that applies in the case of DNWR.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Inputs needed
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% 1. Previous trade shares (PastSha), size I*I*S
% 2. Dot trade costs (TAUmatrix), size I*I*S
% 3. Dot technologies (TECmatrix), size I*S
% 4. Labor shares (labshares), size I*S
% 5. IO matrix (inoutmat), size I*S*S
% 6. Trade elasticities (Smat), size 1*S
% 7. Final good consumption shares (Amat), size I*S
% 8. Previous income by sector (PastYnom), size I*S
% 9. Limit to labor dot (Llim), size I*S
% 10. Limit to wage dot (Wlim), size I*S
% 11. Deficit levels (Deficits), size I
% 12. Max number of iterations (maxiter), scalar
% 13. Tolerance for stopping (tolerance), scalar
% 14. Lambda for tatonnement algorithm (lambda), scalar
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Outputs produced
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% 1. Wages dot (EqWages), size I*S
% 2. Labor dot (EqLabor), size I*S
% 3. Prices dot (EqPrices), size I*S
% 4. New trade share level (EqShares), size I*I*S
% 5. New revenue levels (EqRev), size I*S
% 6. Number of iterations used (iterations), scalar
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%% Matlab functions invoked
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 
% 1. NEBA_PriceSolver
% 2. NEBA_RevenueSolver
% 3. NEBA_TatoNewGuess

function [EqWages,EqLabor,EqPrices,EqShares,EqRev,iterations] = ...
NEBA_PeriodTatoSolver(PastSha,TAUmatrix,TECmatrix,labshares,inoutmat,...
Smat,Amat,PastYnom,Llim,Wlim,Deficits,maxiter,tolerance,lambda)

% Define some important variables

I              = size(Amat,1);
S              = size(Amat,2);
Lng            = ones(I,S);
Wng            = ones(I,S);
iterations     = 0;
distancebg     = 10;

% Run the contraction mapping loop part of the tatonnement algorithm 
% described in the paper

while distancebg>tolerance && iterations<maxiter
    Wg         = Wng;
    Lg         = Lng;
    [Wng,Lng]  = NEBA_TatoNewGuess(PastSha,TAUmatrix,TECmatrix,...
                 labshares,inoutmat,Smat,Amat,PastYnom,Llim,Wlim,...
                 Deficits,Wg,Lg,lambda);          
    iterations = iterations+1;
    distancebg = max(max(abs(Wng(:)-Wg(:))),max(abs(Lng(:)-Lg(:))));
end

% Assign final values to wage and labor dots and retrieve final price dots
% trade shares and revenues

EqWages        = Wng;
EqLabor        = Lng;
[EqPrices,EqShares] = NEBA_PriceSolver(ones(I,S),PastSha,TAUmatrix,...
TECmatrix,EqWages,labshares,inoutmat,Smat,800,1e-8);
Vmat           = sum(EqWages.*EqLabor.*PastYnom,2) + Deficits;
EqRev          = NEBA_RevenueSolver(EqShares,Vmat,Amat,inoutmat);

% Print a warning if iterations are close or at limit

if iterations > maxiter-3
    disp("Maximum number of iterations in tatonnement reached")
end

end
